#File: rondamon_test.py - Rondamon.py unit testing
#JGordon & Co

from unittest import TestCase, makeSuite, TextTestRunner
from rondamon import parse, RondamonException

class RondamonPLYTetsCase(TestCase):
	
	def testEmpty(self):
		"empty test"
		self.assertEquals(parse(' '), [[]])
		
	def testComments(self):
		"comment test"
		self.assertEquals(parse('--This is a comment x = 0'), [[]])
	
	def testString(self):
		"String test"
		self.assertEquals(parse('''--This is a comment \n x = "hola mundo"'''), [[['=','x','"hola mundo"']]])
		
	def testPrint(self):
		"print test"
		self.assertEquals(parse('print 10'), [[['print', 10]]])
		self.assertEquals(parse('print a + 1'), [[['print', ['+', 'a', 1]]]])
		self.assertRaises(RondamonException, parse, 'print (x + 1')
		
	def testAssign(self):
		"assign test"
		self.assertEquals(parse('x = y < b'), [[['=', 'x', ['<','y','b']]]])
		self.assertEquals(parse('x = []'), [[['=', 'x', []]]])
		self.assertEquals(parse('x = 1 + 2 + 3'), [[['=', 'x', ['+', ['+', 1, 2], 3]]]])
        	self.assertRaises(RondamonException, parse, 'x = ')
		self.assertEquals(parse('x = hola(8)'), [[['=', 'x', ['call','hola',[8]]]]])
		
	def testIf(self):
		"if test"
		self.assertEquals(parse('if x then end'), [[['if', 'x', []]]])
		self.assertEquals(parse('if x < fact(3) then end'), [[['if', ['<','x',['call','fact',[3]]], []]]])
		self.assertEquals(parse('if x < #x then end'), [[['if', ['<','x',['#','x']], []]]])
		self.assertEquals(parse('if x ~= 10 then end'), [[['if', ['~=','x', 10],[]]]])
		self.assertEquals(parse('if x == 10 then end'), [[['if', ['==','x', 10],[]]]])
		self.assertEquals(parse('if (x < 5) and (x>2) then x = 10 end'), [[['if', ['and',['<', 'x', 5],['>','x',2]], [['=', 'x', 10]]]]])
		self.assertRaises(RondamonException, parse, 'if x < 5 let y := 1 end')
		
	def testWhile(self):
		"while test"
		self.assertEquals(parse('while x < y do end'), [[['while', ['<','x','y'], []]]])
		self.assertEquals(parse('while (x < 5) do x = 10 end'), [[['while', ['<', 'x', 5], [['=', 'x', 10]]]]])
		
	def testlists(self):
		"lists test"
		self.assertEquals(parse("x = [1,2,3]"),[[['=','x',[1,2,3]]]])
		self.assertEquals(parse("x = #[1,2,3]"),[[['=','x',['#',[1,2,3]]]]])
		#self.assertEquals(parse("x[y]"),[[['index','x','y']]])
		#self.assertEquals(parse("x[2]"),[[['index','x',2]]])
		
	def testProgram1(self):
		"program1"
		source = '''
		--Programa de ejemplo del lenguaje #1
		fu factorial(x)
		  result = 1
		  while x > 0 do
		    result = result * x
		    x = x - 1
		  end
		  print "Fin de la ejecucion"
  		  return result
		end
		
		x = 1
		y = 2
		factorial(x)
		print resultado
		booleano = (x==y) and (y<1)
		'''
		self.assertEquals(parse(source),[[
		['fu','factorial',['x'],
		[['=','result',1],['while', ['>','x',0], 
		[['=','result',['*','result','x']],['=','x',['-','x',1]]]]
		,['print','"Fin de la ejecucion"'],['return','result']]],
		['=','x',1],['=','y',2],
		['call','factorial',['x']],['print','resultado'],
		['=','booleano',['and',['==','x','y'],['<','y',1]]]
		]])
		
TextTestRunner(verbosity=2).run(makeSuite(RondamonPLYTetsCase))